package com.hfzy.ihk.web.freeswitchManage.mapper;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hfzy.ihk.facade.freeswitchManage.entity.Gateway;
import com.hfzy.ihk.facade.freeswitchManage.entity.GatewayCardSlot;
import com.hfzy.ihk.web.freeswitchManage.vo.EquimentSlotInfo;
import com.hfzy.ihk.web.freeswitchManage.vo.GatewayCardSlotVo;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author czs
 * @since 2019-03-22
 */
public interface GatewayCardSlotMapper extends BaseMapper<GatewayCardSlot> {

    @Select("select * from gateway_card_slot where ccid=#{ccid}")
    GatewayCardSlot queryGatewayCardSlotByCcid(@Param("ccid")String ccid);

    /**
     * 通过 sn+卡槽号判断是否存在该卡槽
     */
    @Select("select * from gateway_card_slot where gateway_num=#{sn} and slot_index=#{slotIndex} and channel = #{channel}")
    GatewayCardSlot queryGatewayCardSlotBySnAndSoltIndxAndChannel(@Param("sn")String sn,@Param("slotIndex")String slotIndex,@Param("channel")String channel);

    /**
     * 通过 sn+卡槽号 插卡 更新
     */

    @Update("update gateway_card_slot set slot_state=#{slotState},ccid='' where gateway_num=#{sn} and slot_index=#{slotIndex} and channel = #{channel}  ")
    void updateCardInfoWhenUp(@Param("sn")String sn,@Param("slotIndex")String slotIndex,@Param("channel")String channel,@Param("slotState")String slotState);

    /**
     * 通过 sn+卡槽号 拔卡 更新
     */

    @Update("update gateway_card_slot set ccid=''  ,slot_state=#{slotState} where slot_index=#{slotIndex} and channel = #{channel} and gateway_num=#{sn} ")
    void updateCardInfoWhenDown(@Param("sn")String sn,@Param("slotIndex")String slotIndex,@Param("channel")String channel,@Param("slotState")String slotState);

    /**
     * 激活卡
     * @param sn
     * @param slotIndex
     * @param channel
     * @param ccid
     */
    @Update("update gateway_card_slot set ccid=#{ccid},slot_state=#{slotState} where gateway_num=#{sn} and slot_index=#{slotIndex} and channel = #{channel}  ")
    void updateCardslotActiveState(@Param("sn")String sn,@Param("slotIndex")String slotIndex,@Param("channel")String channel,@Param("ccid")String ccid,@Param("slotState")String slotState);

    @Update("update gateway_card_slot set slot_state=#{slotState} where gateway_num=#{sn}  and channel = #{channel} and  slot_state='3'")
    void updateCardslotNotActiveState(@Param("sn")String sn,@Param("channel")String channel,@Param("slotState")String slotState);

    @Delete("delete from gateway_card_slot where gateway_num =#{sn}")
    void delCardslotBySn( @Param("sn")String sn);

    @Insert("insert into gateway_card_slot(gateway_num,channel,slot_index,slot_state) VALUES(#{equimentSlotInfo.sn},#{equimentSlotInfo.channel},#{equimentSlotInfo.slotId},#{slotState}) ON DUPLICATE KEY UPDATE slot_state= VALUES (slot_state)")
    int saveOrUpdate(@Param("equimentSlotInfo")EquimentSlotInfo equimentSlotInfo,@Param("slotState")String slotState);

    @Update("update gateway_card_slot set slot_state=#{slotState} where gateway_num=#{sn}  and channel = #{channel} and slot_index=#{slotIndex}" )
    int updateSlotState(@Param("sn")String sn,@Param("channel")String channel,@Param("slotIndex")String slotIndex,@Param("slotState")String slotState);




    Integer count(@Param("params") Map<String, Object> params);

    List<GatewayCardSlotVo> list(@Param("params") Map<String, Object> params, @Param("offset") Integer offset,
                                 @Param("limit") Integer limit);


    /**
     * 查询通道中继信息
     * @
     * @param sn
     * @return
     */
    @Select("select s.belonging_to,g.ccid ,s.belonging_company as belongCompany,gw.sip_user,gw.ip ,s.num as phoneNum,gw.state from gateway_card_slot  g left join sim_card  s on  g.ccid=s.ccid  LEFT JOIN gateway gw on gw.num=g.gateway_num where  g.channel = #{channel} and g.gateway_num=#{sn} and g.slot_index=#{slotIndex}")
    List<GatewayCardSlotVo> queryChannelTrunkInfoByTrunkName(@Param("sn")String sn ,@Param("channel")String channel,@Param("slotIndex")String slotIndex);


    /**
     * 查询所有激活通道的中继信息
     * 用于跑job检验
     */
    @Select("select gw.num,g.channel,g.slot_index,s.belonging_to,g.ccid ,s.belonging_company as belongCompany,gw.sip_user,gw.ip,s.num as phoneNum,gw.state from gateway_card_slot  g left join sim_card  s on  g.ccid=s.ccid  LEFT JOIN gateway gw on gw.num=g.gateway_num where  g.slot_state=3")
    List<GatewayCardSlotVo> queryAllActiveChannelTrunkInfo();

}
